package com.netposa.config;

import com.alibaba.druid.pool.DruidDataSource;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;

public class JdbcDatasourceConfigure {
   private static final Logger log = LoggerFactory.getLogger(JdbcDatasourceConfigure.class);
   public static final String urlPreffix = "jdbc:mysql://%s/%s?autoReconnect=true&useSSL=false&characterEncoding=UTF-8&createDatabaseIfNotExist=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&rewriteBatchedStatements=true";
   public static final String driverClassName = "com.mysql.jdbc.Driver";

   public static String assemblyUrl(String address, String name) {
      return String.format("jdbc:mysql://%s/%s?autoReconnect=true&useSSL=false&characterEncoding=UTF-8&createDatabaseIfNotExist=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&rewriteBatchedStatements=true", address, name);
   }

   @Configuration
   @MapperScan(
      basePackages = {"com.netposa.mapper.db0"},
      sqlSessionFactoryRef = "db0SqlSessionFactory"
   )
   @ConfigurationProperties(
      prefix = "netposa.db0"
   )
   public static class Db0DataSourceConfigure {
      private String driverClassName;
      private String address;
      private String name;
      private String username;
      private String password;
      private boolean mapUnderscoreToCamelCase;
      private String mapperLocations;
      private Properties properties = new Properties();

      @Primary
      @Bean(
         name = {"db0DataSource"}
      )
      @Qualifier("db0DataSource")
      public DataSource pvmDataSource() {
         this.properties.setProperty("maxActive", "3");
         this.properties.setProperty("maxIdle", "1");
         this.properties.setProperty("maxWaitMillis", "10000");
         this.properties.setProperty("minIdle", "1");
         this.properties.setProperty("initialSize", "1");
         this.properties.setProperty("validationQuery", "SELECT 1 FROM DUAL");
         this.properties.setProperty("testOnBorrow", "false");
         this.properties.setProperty("testOnBorrow", "false");
         this.properties.setProperty("testWhileIdle", "true");
         this.properties.setProperty("timeBetweenEvictionRunsMillis", "18800");
         DruidDataSource dataSource = new DruidDataSource();
         dataSource.configFromPropety(this.properties);
         dataSource.setUsername(this.username);
         dataSource.setPassword(this.password);
         dataSource.setUrl(JdbcDatasourceConfigure.assemblyUrl(this.address, this.name));
         dataSource.setDriverClassName(this.driverClassName);
         return dataSource;
      }

      @Primary
      @Bean(
         name = {"db0JdbcTemplate"}
      )
      public JdbcTemplate pvmJdbcTemplate(@Qualifier("db0DataSource") DataSource dataSource) {
         return new JdbcTemplate(dataSource);
      }

      @Primary
      @Bean(
         name = {"db0SqlSessionFactory"}
      )
      @Qualifier("db0SqlSessionFactory")
      public SqlSessionFactory db0SqlSessionFactory(@Qualifier("db0DataSource") DataSource dataSource) throws Exception {
         SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
         bean.setDataSource(dataSource);
         org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
         configuration.setMapUnderscoreToCamelCase(true);
         bean.setConfiguration(configuration);
         bean.setMapperLocations((new PathMatchingResourcePatternResolver()).getResources(this.mapperLocations));
         return bean.getObject();
      }

      public String getDriverClassName() {
         return this.driverClassName;
      }

      public String getAddress() {
         return this.address;
      }

      public String getName() {
         return this.name;
      }

      public String getUsername() {
         return this.username;
      }

      public String getPassword() {
         return this.password;
      }

      public boolean isMapUnderscoreToCamelCase() {
         return this.mapUnderscoreToCamelCase;
      }

      public String getMapperLocations() {
         return this.mapperLocations;
      }

      public Properties getProperties() {
         return this.properties;
      }

      public void setDriverClassName(String driverClassName) {
         this.driverClassName = driverClassName;
      }

      public void setAddress(String address) {
         this.address = address;
      }

      public void setName(String name) {
         this.name = name;
      }

      public void setUsername(String username) {
         this.username = username;
      }

      public void setPassword(String password) {
         this.password = password;
      }

      public void setMapUnderscoreToCamelCase(boolean mapUnderscoreToCamelCase) {
         this.mapUnderscoreToCamelCase = mapUnderscoreToCamelCase;
      }

      public void setMapperLocations(String mapperLocations) {
         this.mapperLocations = mapperLocations;
      }

      public void setProperties(Properties properties) {
         this.properties = properties;
      }

      public boolean equals(Object o) {
         if (o == this) {
            return true;
         } else if (!(o instanceof Db0DataSourceConfigure)) {
            return false;
         } else {
            Db0DataSourceConfigure other = (Db0DataSourceConfigure)o;
            if (!other.canEqual(this)) {
               return false;
            } else {
               Object this$driverClassName = this.getDriverClassName();
               Object other$driverClassName = other.getDriverClassName();
               if (this$driverClassName == null) {
                  if (other$driverClassName != null) {
                     return false;
                  }
               } else if (!this$driverClassName.equals(other$driverClassName)) {
                  return false;
               }

               Object this$address = this.getAddress();
               Object other$address = other.getAddress();
               if (this$address == null) {
                  if (other$address != null) {
                     return false;
                  }
               } else if (!this$address.equals(other$address)) {
                  return false;
               }

               Object this$name = this.getName();
               Object other$name = other.getName();
               if (this$name == null) {
                  if (other$name != null) {
                     return false;
                  }
               } else if (!this$name.equals(other$name)) {
                  return false;
               }

               label78: {
                  Object this$username = this.getUsername();
                  Object other$username = other.getUsername();
                  if (this$username == null) {
                     if (other$username == null) {
                        break label78;
                     }
                  } else if (this$username.equals(other$username)) {
                     break label78;
                  }

                  return false;
               }

               label71: {
                  Object this$password = this.getPassword();
                  Object other$password = other.getPassword();
                  if (this$password == null) {
                     if (other$password == null) {
                        break label71;
                     }
                  } else if (this$password.equals(other$password)) {
                     break label71;
                  }

                  return false;
               }

               if (this.isMapUnderscoreToCamelCase() != other.isMapUnderscoreToCamelCase()) {
                  return false;
               } else {
                  Object this$mapperLocations = this.getMapperLocations();
                  Object other$mapperLocations = other.getMapperLocations();
                  if (this$mapperLocations == null) {
                     if (other$mapperLocations != null) {
                        return false;
                     }
                  } else if (!this$mapperLocations.equals(other$mapperLocations)) {
                     return false;
                  }

                  Object this$properties = this.getProperties();
                  Object other$properties = other.getProperties();
                  if (this$properties == null) {
                     if (other$properties != null) {
                        return false;
                     }
                  } else if (!this$properties.equals(other$properties)) {
                     return false;
                  }

                  return true;
               }
            }
         }
      }

      protected boolean canEqual(Object other) {
         return other instanceof Db0DataSourceConfigure;
      }

      public int hashCode() {
         
         int result = 1;
         Object $driverClassName = this.getDriverClassName();
         result = result * 59 + ($driverClassName == null ? 43 : $driverClassName.hashCode());
         Object $address = this.getAddress();
         result = result * 59 + ($address == null ? 43 : $address.hashCode());
         Object $name = this.getName();
         result = result * 59 + ($name == null ? 43 : $name.hashCode());
         Object $username = this.getUsername();
         result = result * 59 + ($username == null ? 43 : $username.hashCode());
         Object $password = this.getPassword();
         result = result * 59 + ($password == null ? 43 : $password.hashCode());
         result = result * 59 + (this.isMapUnderscoreToCamelCase() ? 79 : 97);
         Object $mapperLocations = this.getMapperLocations();
         result = result * 59 + ($mapperLocations == null ? 43 : $mapperLocations.hashCode());
         Object $properties = this.getProperties();
         result = result * 59 + ($properties == null ? 43 : $properties.hashCode());
         return result;
      }

      public String toString() {
         return "JdbcDatasourceConfigure.Db0DataSourceConfigure(driverClassName=" + this.getDriverClassName() + ", address=" + this.getAddress() + ", name=" + this.getName() + ", username=" + this.getUsername() + ", password=" + this.getPassword() + ", mapUnderscoreToCamelCase=" + this.isMapUnderscoreToCamelCase() + ", mapperLocations=" + this.getMapperLocations() + ", properties=" + this.getProperties() + ")";
      }
   }
}
